var isAdd = true;
var vm = new Vue({
    el:"#main",
    data:{
        tabType:0,
        tableHeight:"",
        treeKey:'',
        dialogTitle:'',
        categories:[],
        tab1:{
            search:{
                current: 1,
                size: 10,
                code:"",
                des:"",
                level:""
            },
            editDisabled:false,
            treeData:[],
            levels:[],
            list:[],
            total:0,
            listLoading:true,
            tableData:[],
            addVisible:false,
            form:{
                id:"",
                code:"",
                currentLevel:"",
                englishDescription:"",
                chineseDescription:"",
                parentCode:"",
                parentLevel:"",
            },
        },
        tab2:{
            isFirst:true,
            search:{
                current: 1,
                size: 10,
                codes:[],
                cms:[],
                status:0
            },
            listLoading:false,
            statusArr:[
                {"value":0,"label":'All'},
                {"value":1,"label":'Yes'},
                {"value":2,"label":'No'}
            ],
            total:0,
            tableData:[],
            addVisible:false,
            form:{
                id:"",
                categoryCode:"",
                manager:""
            },
            cms:[]
        },
        tab3:{
            isFirst:true,
            search:{
                current: 1,
                size: 10,
                codes:[],
                users:[],
                status:0
            },
            listLoading:false,
            statusArr:[
                {"value":0,"label":'All'},
                {"value":1,"label":'Yes'},
                {"value":2,"label":'No'}
            ],
            total:0,
            tableData:[],
            addVisible:false,
            form:{
                id:"",
                categoryCode:"",
                sscme:"",
                sqme:""
            },
            sscmes:[],
            sqmes:[],
            searchUsers:[]
        }
    },
    watch: {
        treeKey:function(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods:{
        changeTab:function (tab) {
            console.log(tab.index);
            this.tabType=tab.index;
            if(tab.index==1){
                this.getCategoryManager();
                if(this.tab2.isFirst){
                    this.tab2.isFirst = false;
                    this.getAllCM();
                    this.getAllCategory();
                }
            }else if(tab.index==2){
                this.getCategoryEngineer();
                if(this.tab3.isFirst){
                    this.tab3.isFirst = false;
                    this.getAllSSCME();
                    this.getAllSQME();
                    this.getAllSearchUser();
                    this.getAllCategory();
                }
            }
        },
        getAllCategory:function(){
            VanyAjax("/data/categoryList/queryByLevel",{"level":'Category'}, "POST").done(function (data) {
                this.categories=data;
            }, false, this);
        },
        getTreeCategory:function () {
            VanyAjax("/data/categoryList/tree", {"key":this.treeKey}, "POST").done(function (data) {
                this.tab1.treeData = data;
            }, false, this);
        },
        filterNode:function(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        getCategoryList:function () {
            this.tab1.listLoading = true;
            VanyAjax("/data/categoryList/list", this.tab1.search, "POST").done(function (data) {
                this.tab1.total = data.total;
                this.tab1.tableData=data.records;
                this.tab1.listLoading = false;
            }, false, this);
        },
        getLevels:function(){
            VanyAjax("/data/categoryLevel/all",{}, "GET").done(function (data) {
                this.tab1.levels=data;
            }, false, this);
        },
        changeLevel:function(){
            var current;
            var levels = this.tab1.levels;
            this.tab1.form.parentCode="";
            for(i in levels){
                if(levels[i].name==this.tab1.form.currentLevel){
                    current = levels[i];
                    break;
                }
            }
            if(current.pid!=0){
                var parent;
                for(i in levels){
                    if(levels[i].id==current.pid){
                        parent = levels[i];
                        break;
                    }
                }
                this.tab1.form.parentLevel=parent.name;
                VanyAjax("/data/categoryList/queryByLevel",{"level":parent.name}, "POST").done(function (data) {
                    this.tab1.list=data;
                }, false, this);
            }else{
                this.tab1.form.parentLevel="";
            }
        },
        addCategory:function(){
            this.dialogTitle=$("#addTitle").val();
            this.tab1.editDisabled=false;
            isAdd = true;
            this.tab1.form.id=""
            this.tab1.form.code="";
            this.tab1.form.currentLevel="";
            this.tab1.form.englishDescription="";
            this.tab1.form.chineseDescription="";
            this.tab1.form.parentCode="";
            this.tab1.form.parentLevel="";
            this.tab1.addVisible=true;
        },
        editCategory:function (row) {
            this.dialogTitle=$("#editTitle").val();
            this.tab1.editDisabled=true;
            isAdd = false;
            this.tab1.form.id=row.id;
            this.tab1.form.code=row.code;
            this.tab1.form.currentLevel=row.currentLevel;
            this.tab1.form.englishDescription=row.englishDescription;
            this.tab1.form.chineseDescription=row.chineseDescription;
            this.tab1.form.parentCode=row.parentCode;
            this.tab1.form.parentLevel=row.parentLevel;
            this.tab1.addVisible=true;
        },
        submitCategory:function () {
            var url = isAdd?"/data/categoryList/save":"/data/categoryList/update";
            VanyAjax(url,this.tab1.form, "POST").done(function (data) {
                this.tab1.addVisible=false;
                this.getCategoryList();
            }, true, this);
        },
        deleteCategory:function (row) {
            this.$confirm('Confirm deletion?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                VanyAjax("/data/categoryList/delete",{"id":row.id}, "POST").done(function (data) {
                    vm.getData();
                }, false, this);
            }).catch(function() {})
        },
        mask:function(row){
            VanyAjax("/data/categoryList/mask",{id:row.id,mask:row.mask}, "POST").done(function (data) {

            }, false, this);
        },
        exportCategory:function(){
            // VanyAjax("/data/plant/list", {
            //     current: this.pageIndex,
            //     size: this.pageSize,
            //     key: this.search,
            //     export:true
            // }, "POST").done(function (data) {
            //
            // }, false, this);
        },
        getCategoryManager:function(){
            this.tab2.listLoading = true;
            VanyAjax("/data/categoryManager/list",this.tab2.search, "POST").done(function (data) {
                this.tab2.total = data.total;
                this.tab2.tableData=data.records;
                this.tab2.listLoading = false;
            }, false, this);
        },
        getAllCM:function () {
            VanyAjax("/sys/user/selectByRoleIds?roleIds=CM",{}, "GET").done(function (data) {
                this.tab2.cms=data;
            }, false, this);
        },
        editCategoryManager:function(row){
            this.dialogTitle=$("#editTitle").val();
            this.tab2.form.id=row.id;
            this.tab2.form.categoryCode=row.code;
            this.tab2.form.manager=row.manager;
            this.tab2.addVisible=true;
        },
        submitCategoryManager:function () {
            VanyAjax("/data/categoryManager/saveOrUpdate",this.tab2.form, "POST").done(function (data) {
                this.tab2.addVisible=false;
                this.getCategoryManager();
            }, true, this);
        },
        getCategoryEngineer:function(){
            this.tab3.listLoading = true;
            VanyAjax("/data/categoryEngineer/list",this.tab3.search, "POST").done(function (data) {
                this.tab3.total = data.total;
                this.tab3.tableData=data.records;
                this.tab3.listLoading = false;
            }, false, this);
        },
        editCategoryEngineer:function(row){
            this.dialogTitle=$("#editTitle").val();
            this.tab3.form.id=row.id;
            this.tab3.form.categoryCode=row.code;
            this.tab3.form.sscme=row.sscme;
            this.tab3.form.sqme=row.sqme;
            this.tab3.addVisible=true;
        },
        getAllSSCME:function () {
            VanyAjax("/sys/user/selectByRoleIds?roleIds=SSCME",{}, "GET").done(function (data) {
                this.tab3.sscmes=data;
            }, false, this);
        },
        getAllSQME:function () {
            VanyAjax("/sys/user/selectByRoleIds?roleIds=SQL",{}, "GET").done(function (data) {
                this.tab3.sqmes=data;
            }, false, this);
        },
        getAllSearchUser:function(){
            VanyAjax("/sys/user/selectByRoleIds?roleIds=SSCME,SQL",{}, "GET").done(function (data) {
                this.tab3.searchUsers=data;
            }, false, this);
        },
        submitCategoryEngineer:function () {
            VanyAjax("/data/categoryEngineer/saveOrUpdate",this.tab3.form, "POST").done(function (data) {
                this.tab3.addVisible=false;
                this.getCategoryEngineer();
            }, true, this);
        },
    },
    mounted:function () {
        this.tableHeight = window.innerHeight - 450;
        this.getTreeCategory()
        this.getCategoryList();
        this.getLevels();
    }
})
